//
//  SocketMethod.m
//  TengyunStation
//
//  Created by Johnny on 15/8/21.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "SocketMethod.h"
#import "C#Library.h"
#import "ComMethod.h"

#define CheckCode               @"DSMS"
#define IOS_Type                @"0"
#define FourByteLength          4
#define TwoByteLength           2

@implementation SocketMethod

+ (NSData *)socketHeadDataMethod:(NSString *)method {
    NSMutableData *mutableData = [[NSMutableData alloc] init];
    [mutableData appendBytes:[CheckCode UTF8String] length:4];
    [mutableData appendBytes:[SocketMethod checkLegal:method] length:4];
    [mutableData appendBytes:[IOS_Type UTF8String] length:1];
    return mutableData;
}

//计算crc16校验码
+ (NSData *)getCRC16WithData:(NSData *)data {
    unsigned short t = usCRC16_tbl((unsigned char *)data.bytes, (int)data.length);
    char crc16[TwoByteLength + 1];
    VShort2Byte(t, crc16);
    return [[NSData alloc] initWithBytes:crc16 length:TwoByteLength];
}

//Data转长度
+ (int)getLengthWithData:(NSData *)data {
    char *intBye = (char *)[data bytes];
    int dataLength = 0;
    VByte2Int(&dataLength, intBye);
    return dataLength;
}

+ (NSData *)hexDataWithLength:(NSInteger)length {
    char byte[FourByteLength + 1];
    VInt2Byte((int)length, byte);
    return [NSData dataWithBytes:byte length:FourByteLength];
}

+ (NSData *)doubleHexEncryptMessageData:(NSData *)data {
    NSMutableData *firstData = [[NSMutableData alloc] initWithData:data];
    //firstData末拼接2字节的crc16校验码
    [firstData appendData:[SocketMethod getCRC16WithData:data]];
    //数据前置拼接4字节的firstData长度
    NSMutableData *secondData = [NSMutableData dataWithData:[SocketMethod hexDataWithLength:firstData.length]];
    [secondData appendData:firstData];
    //第二次数据前置拼接4字节的secondData长度
    NSMutableData *thirdData = [NSMutableData dataWithData:[SocketMethod hexDataWithLength:secondData.length]];
    [thirdData appendData:secondData];
    return thirdData;
}

+ (NSData *)messageDataDecryptHexData:(NSData *)data {
    //计算后续报文长度
    NSData *lengthData = [data subdataWithRange:NSMakeRange(0, FourByteLength)];
    int length = [SocketMethod getLengthWithData:lengthData];
    NSData *crcAndBwData = [data subdataWithRange:NSMakeRange(FourByteLength, length)];
    NSData *bwData = [crcAndBwData subdataWithRange:NSMakeRange(0, crcAndBwData.length - TwoByteLength)];
    return bwData;
}

//nsdata转字符串
+ (NSString *)stringData:(NSData *)data {
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return [string stringByReplacingOccurrencesOfString:@" " withString:@""];
}

//前置一次16进制data转字符串
+ (NSString *)stringDecryHexData:(NSData *)data {
    int length = [SocketMethod getLengthWithData:[data subdataWithRange:NSMakeRange(0, FourByteLength)]];
    NSData *contentData = [data subdataWithRange:NSMakeRange(FourByteLength, length)];
    return [[NSString alloc] initWithData:contentData encoding:NSUTF8StringEncoding];
}

+ (NSString *)stringDecryHexData:(NSData *)data Range:(NSRange)range {
    if (data.length >= (range.location + range.length)) {
        NSData *contentData = [data subdataWithRange:range];
        return [SocketMethod stringDecryHexData:contentData];
    }
    return @"";
}

//nsdata截取返回长度
+ (NSInteger)integerSubdata:(NSData *)data Range:(NSRange)range {
    if (data.length >= (range.location + range.length)) {
        NSData *contentData = [data subdataWithRange:range];
        return [SocketMethod getLengthWithData:contentData];
    }
    return 0;
}


+ (NSString *)stringSubdata:(NSData *)data Range:(NSRange)range {
    if (data.length >= (range.location + range.length)) {
        NSData *contentData = [data subdataWithRange:range];
        return [SocketMethod stringData:contentData];
    }
    return @"";
}

+ (NSString *)subArray:(NSArray *)array Index:(NSInteger)index {
    if (array.count > index) {
        if ([array objectAtIndex:index] != [NSNull null]) {
            return [array objectAtIndex:index];
        }
    }
    return @"";
}

+ (NSString *)getThisMonTypeyyyyMM {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyyMM";
    return [dateFormatter stringFromDate:[NSDate date]];
}

+ (NSString *)getLastMonTypeyyyyMM {
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:[NSDate date]];
    NSInteger month = dateComponents.month;
    dateComponents.month = month - 1;
    NSDate *date = [greCalendar dateFromComponents:dateComponents];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyyMM";
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)expressType:(NSString *)type {
    if ([type isEqualToString:@"0001"]) {
        return @"待录入";
    } else if ([type isEqualToString:@"0002"]) {
        return @"待自提";
    } else if ([type isEqualToString:@"0005"]) {
        return @"待配送";
    } else if ([type isEqualToString:@"0006"]) {
        return @"待自提";
    } else if ([type isEqualToString:@"0007"]) {//已取
        return @"客户签收";
    } else if ([type isEqualToString:@"0008"]) {
        return @"取消投递";
    } else if ([type isEqualToString:@"0011"]) {
        return @"快递柜已取件";
    } else if ([type isEqualToString:@"0012"]) {
        return @"回仓";
    } else if ([type isEqualToString:@"0015"]) {
        return @"客户自签";
    } else if ([type isEqualToString:@"0023"]) {
        return @"异常件";
    } else if ([type isEqualToString:@"0029"]) {
        return @"已退件";
    } else if ([type isEqualToString:@"0031"]) {//待出仓
        return @"待配送";
    } else if ([type isEqualToString:@"0032"]) {
        return @"待入柜";
    } else if ([type isEqualToString:@"0033"]) {//已取
        return @"小站代签";
    } else if ([type isEqualToString:@"0041"]) {
        return @"存件";
    } else if ([type isEqualToString:@"0042"]) {
        return @"普通取件";
    } else if ([type isEqualToString:@"0043"]) {
        return @"微信取件";
    } else if ([type isEqualToString:@"0044"]) {
        return @"取消存件";
    }  else if ([type isEqualToString:@"0045"]) {
        return @"取逾期件";
    }  else if ([type isEqualToString:@"0046"]) {
        return @"管理员开柜";
    }
    return  nil;
}

//转字节时检测数据是否为nil
+ (const char *)checkLegal:(NSString *)string {
    return [[SocketMethod checkNull:string] UTF8String];
}

//检测字符串是否为空
+ (NSString *)checkNull:(NSString *)string {
    if (string) {
        return string;
    } else {
        return @"";
    }
}

+ (NSString *)dateStringWithDateString:(NSString *)string {
    if ([string rangeOfString:@"-"].location != NSNotFound || string.length <= 0) {
        return string;
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyyMMddHHmmss";
    if (string.length > 14) {
        string = [string substringToIndex:14];
    }
    NSDate *date = [dateFormatter dateFromString:string];
    NSDateFormatter *tranDateFormatter = [[NSDateFormatter alloc] init];
    tranDateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [tranDateFormatter stringFromDate:date];
}

+ (NSString *)getSocketVersion {
    NSString *version = [ComMethod getVersion];
    NSString *headString = @"V0000000";
    NSString *socketVersion = [NSString stringWithFormat:@"%@%@",[headString substringToIndex:(headString.length - version.length)],version];
    return socketVersion;
}

@end
